library(tidyverse)

setwd("/Users/dmin/Dropbox/Replication_Data/Paper_2/")

Dat <- read.csv("RawData.csv", stringsAsFactors = FALSE)

###  Determine respondent threshold for counterforce approval 

Dat <-
  Dat %>%
  mutate(
  # First Strike
    Confirm0 = if_else(is.na(Confirm0),NA_real_, 0),
    Confirm01 = if_else(is.na(Confirm01),NA_real_, 1),
    Confirm10 = if_else(is.na(Confirm10),NA_real_, 10),
    Confirm20 = if_else(is.na(Confirm20),NA_real_, 20),
    Confirm30 = if_else(is.na(Confirm30),NA_real_, 30),
    Confirm40 = if_else(is.na(Confirm40),NA_real_, 40),
    Confirm50 = if_else(is.na(Confirm50),NA_real_, 50),
    Confirm60 = if_else(is.na(Confirm60),NA_real_, 60),
    Confirm70 = if_else(is.na(Confirm70),NA_real_, 70),
    Confirm80 = if_else(is.na(Confirm80),NA_real_, 80),
    Confirm90 = if_else(is.na(Confirm90),NA_real_, 90),
    Confirm99 = if_else(is.na(Confirm99),NA_real_, 99),
    Confirm100 = if_else(is.na(Confirm100),NA_real_, 100),
    ConfirmNever = if_else(is.na(ConfirmNever),NA_real_, 110),
    outcomeFS = coalesce(
      Confirm0, Confirm01, Confirm10, Confirm20, Confirm30, Confirm40, Confirm50, Confirm60, 
      Confirm70, Confirm80, Confirm90, Confirm99,Confirm100, ConfirmNever),
    outcomeFS_custom = ifelse(is.na(ConfirmCustom_1),outcomeFS,ConfirmCustom_1),
  # Second Strike  
    NConfirm0 = if_else(is.na(NConfirm0),NA_real_, 0),
    NConfirm01 = if_else(is.na(NConfirm01),NA_real_, 1),
    NConfirm10 = if_else(is.na(NConfirm10),NA_real_, 10),
    NConfirm20 = if_else(is.na(NConfirm20),NA_real_, 20),
    NConfirm30 = if_else(is.na(NConfirm30),NA_real_, 30),
    NConfirm40 = if_else(is.na(NConfirm40),NA_real_, 40),
    NConfirm50 = if_else(is.na(NConfirm50),NA_real_, 50),
    NConfirm60 = if_else(is.na(NConfirm60),NA_real_, 60),
    NConfirm70 = if_else(is.na(NConfirm70),NA_real_, 70),
    NConfirm80 = if_else(is.na(NConfirm80),NA_real_, 80),
    NConfirm90 = if_else(is.na(NConfirm90),NA_real_, 90),
    NConfirm99 = if_else(is.na(NConfirm99),NA_real_, 99),
    NConfirm100 = if_else(is.na(NConfirm100),NA_real_, 100),
    NConfirmNever = if_else(is.na(NConfirmNever),NA_real_, 110),
    outcomeSS = coalesce(
      NConfirm0, NConfirm01, NConfirm10, NConfirm20, NConfirm30, NConfirm40, NConfirm50, NConfirm60, 
      NConfirm70, NConfirm80, NConfirm90, NConfirm99,NConfirm100, NConfirmNever),
    outcomeSS_custom = ifelse(is.na(NConfirmCustom_1),outcomeSS,NConfirmCustom_1),
  # Combined outcomes
    outcome = coalesce(outcomeFS, outcomeSS),
    outcome_custom = coalesce(outcomeFS_custom, outcomeSS_custom)
  )

# Drop NAs (Respondents who did not get to the threshold question)

Dat <-
  Dat %>% 
  filter(!is.na(outcome_custom))

# Sort respondents into Quadrants

Dat <-
  Dat %>% 
  mutate(
    Quadrant = case_when(
      RiskAvg <= mean(RiskAvg) & UtilAvg <= mean(UtilAvg) ~ 1,
      RiskAvg > mean(RiskAvg) & UtilAvg <= mean(UtilAvg) ~ 2,
      RiskAvg <= mean(RiskAvg) & UtilAvg > mean(UtilAvg) ~ 3,
      RiskAvg > mean(RiskAvg) & UtilAvg > mean(UtilAvg) ~ 4,
    )
  )

# Create additional variables
Dat <-
  Dat %>%
  mutate(
    Nuclear = as.numeric(outcome_custom != 110),
    PartyID = case_when(
      Party==1 ~ 0,
      Party==2 ~ 1,
      Party==3 & PartyCloser==2 ~ 0,
      Party==3 & PartyCloser==1 ~ 1,
      Party==4 & PartyCloser==2 ~ 0,
      Party==4 & PartyCloser==1 ~ 1
    )
  )
      
# Factors for Nuclear Theory
  
Dat <-
    Dat %>%
      mutate(NuclearTheory_fac = factor(
        NuclearTheory,
        levels = 1:4,
        labels = c(
          "Never-Evers",
          "Existential\nDeterrence",
          "Offense/\nDefense",
          "Utilitarian"
        )
      )) %>% 
      filter(!is.na(NuclearTheory_fac))


# Factors for Quadrants

Dat <-
  Dat %>%
  mutate(
    Quadrant_fac = factor(
      Quadrant,
      levels = 1:4,
      labels = c(
    "Low Harm, Low Risk",
    "Low Harm, High Risk",
    "High Harm, Low Risk",
    "High Harm, High Risk"
      )
    )) %>% 
  filter(!is.na(Quadrant_fac))

CLEANdat <- # Selects columns for export
  Dat %>% 
  select(
    ResponseId,
    UtilAvg,
    RiskAvg,
    NuclearTheory,
    AttackType,
    Nuclear,
    NuclearTheory_fac,
    outcomeFS_custom,
    outcomeSS_custom,
    outcome,
    outcome_custom,
    AntiNuclearAll,
    AntiNuclearMost,
    ProNuclearAll,
    ProNuclearMost,
    FreeResponse,
    HeartAttacks,
    Age,
    Party,
    PartyCloser,
    PartyID,
    Gender,
    Ideology,
    Income,
    Military,
    Quadrant,
    Quadrant_fac
  )

write.csv(CLEANdat,"Paper_2_Data_Clean.csv", row.names = FALSE)


